﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ConceptCorporateAdmin.Gui;
using System.Diagnostics;

namespace ConceptCorporateAdmin
{
    public partial class FrmMain : Form
    {
        
        
        public FrmMain()
        {
            InitializeComponent();
            //refresth la lista de categorii
            TvLista.RefreshLista();
            //fa disable editorul
            DEditor.Enabled = false;
        }

        

        private void BtnAdaugaCategorie_Click(object sender, EventArgs e)
        {
            if (TvLista.SelectedExNode != null)
            {
                if (TvLista.SelectedExNode.TipNod == TipuriNoduri.Categorie)
                {
                    String nume = InputBox.ShowMe("Numele categoriei:");
                    if (!nume.Trim().Equals(""))
                        TvLista.AddCategorie(nume); ////
                }
                else
                {
                    MessageBox.Show("Va rugam alegeti o categorie!");
                }
            }
            else
            {
                MessageBox.Show("Va rugam alegeti o categorie!");
            }
        }

        private void BtnSterge_Click(object sender, EventArgs e)
        {
            TvLista.RemoveSelectedNode();
        }

        private void BtnAdaugaActNormativ_Click(object sender, EventArgs e)
        {
            if (TvLista.SelectedExNode != null)
            {
                if (TvLista.SelectedExNode.TipNod == TipuriNoduri.Categorie)
                {
                    int id_categorie  = ((DbCategorie)TvLista.SelectedExNode.InnerObject).Id;
                    DbActNormativ act = Gui.Dialogs.ActNormativ.ShowNew(id_categorie);
                    if (act != null)
                    {
                        ExTreeNode nod = new ExTreeNode(act.Nume, TipuriNoduri.ActeNormative, act);
                        int index      = 0;
                        foreach (TreeNode ntemp in TvLista.SelectedExNode.Nodes)
                        {
                            ExTreeNode enode = (ExTreeNode)ntemp;
                            if (enode.TipNod == TipuriNoduri.Categorie)
                                index++;
                            else
                                break;
                        }
                        TvLista.SelectedExNode.Nodes.Insert(index, nod);
                    }
                }
            }
        }

        private void TvLista_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (TvLista.SelectedExNode != null)
            {
                if (TvLista.SelectedExNode.TipNod == TipuriNoduri.ActeNormative)
                {
                    DbActNormativ act_normativ = (DbActNormativ)TvLista.SelectedExNode.InnerObject;
                    DEditor.ActNormativ = act_normativ;
                    DEditor.Enabled     = true;
                }
                else
                    DEditor.Enabled = false;
            }
        }

        private void TvLista_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (TvLista.SelectedExNode != null)
                {
                    if (TvLista.SelectedExNode.TipNod == TipuriNoduri.ActeNormative)
                        MeniuActNormativ.Show(TvLista, e.X, e.Y);
                    else if (TvLista.SelectedExNode.TipNod == TipuriNoduri.Categorie)
                        MeniuCategorie.Show(TvLista, e.X, e.Y);
                }
            }
        }

        private void editeazaActToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (TvLista.SelectedExNode != null)
            {
                if (TvLista.SelectedExNode.TipNod == TipuriNoduri.ActeNormative)
                {
                    DbActNormativ act = (DbActNormativ)TvLista.SelectedExNode.InnerObject;
                    int id_act       = act.Id;
                    int id_cat       = act.IdCategorie;
                    act              = Gui.Dialogs.ActNormativ.ShowEdit(id_cat, id_act);
                    if (act != null)
                    {
                        TvLista.SelectedExNode.InnerObject = act;
                        TvLista.SelectedExNode.Text = act.Nume;
                    }
                }
            }
        }

        private void BtnTipuriActeNormative_Click(object sender, EventArgs e)
        {
            Gui.Dialogs.TipActNormativ tip = new ConceptCorporateAdmin.Gui.Dialogs.TipActNormativ();
            tip.Show();
        }

        private void editeazaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (TvLista.SelectedExNode != null)
            {
                if (TvLista.SelectedExNode.TipNod == TipuriNoduri.Categorie)
                {
                    DbCategorie cat = (DbCategorie)TvLista.SelectedExNode.InnerObject;
                    String ret      = InputBox.ShowMe("Nume:", cat.Nume);
                    if (!ret.Equals(""))
                    {
                        cat.Nume = ret;
                        cat.Update();
                        DbCategorii.Instance.RefreshTable();
                        TvLista.SelectedExNode.InnerObject = cat;
                        TvLista.SelectedExNode.Text = cat.Nume;
                    }
                }
            }
        }

        private void BtnAddRootNode_Click(object sender, EventArgs e)
        {

            if (TvLista.SelectedExNode != null)
            {
                String nume = InputBox.ShowMe("Numele categoriei:");
                if (!nume.Trim().Equals(""))
                    TvLista.AddCategorieRoot(nume); ////
            }

        }

        private void adaugaSubcategorieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BtnAdaugaCategorie_Click(sender, e);   
        }

        private void stergeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BtnSterge_Click(sender, e);
        }

        private void adaugaActNormativToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BtnAdaugaActNormativ_Click(sender, e);
        }

        private void ordoneazaActeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TvLista.orderActeFromNode(TvLista.SelectedExNode);
        }

    }
}
